package com.myc.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myc.train.common.context.LoginMemberContext;
import com.myc.train.common.resp.PageResp;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.member.domain.Passenger;
import com.myc.train.member.domain.PassengerExample;
import com.myc.train.member.mapper.PassengerMapper;
import com.myc.train.member.req.PassengerQueryReq;
import com.myc.train.member.req.PassengerSaveReq;
import com.myc.train.member.resp.PassengerQueryResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PassengerService {

    @Resource
    private PassengerMapper passengerMapper;

    public void save(PassengerSaveReq req){
        DateTime now = DateTime.now();
        Passenger passenger = BeanUtil.copyProperties(req, Passenger.class);

        if(ObjectUtil.isNull(passenger.getId())){
            passenger.setId(SnowUtil.getSnowflakeNextId());
            passenger.setMemberId(LoginMemberContext.getId());
            passenger.setCreateTime(now);
            passenger.setUpdateTime(now);
            passengerMapper.insert(passenger);
        }else{
            passenger.setUpdateTime(now);
            passengerMapper.updateByPrimaryKey(passenger);
        }
    }

    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq req){
       //定义查询条件
        PassengerExample passengerExample = new PassengerExample();
        //根据id倒序排序
        passengerExample.setOrderByClause("id desc");
        //创建查询对象
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        //不为空则对其新增查询条件
        if(ObjectUtil.isNull(req.getMemberId())){
            criteria.andMemberIdEqualTo(req.getMemberId());
        }
        //新增分页信息
        PageHelper.startPage(req.getPage(),req.getSize());
        //根据条件查询结果
        List<Passenger> passengers = passengerMapper.selectByExample(passengerExample);
        //new一个PageInfo并将结果放进来
        PageInfo<Passenger> passengerPageInfo = new PageInfo<>(passengers);
        //类拷贝
        List<PassengerQueryResp> queryResps = BeanUtil.copyToList(passengers, PassengerQueryResp.class);
        //new一个PageResp
        PageResp<PassengerQueryResp> resp= new PageResp<>();
        //放数据
        resp.setTotal(passengerPageInfo.getTotal());
        resp.setList(queryResps);


        return resp;
    }

    public void delete(Long id){
        passengerMapper.deleteByPrimaryKey(id);
    }

    //查找我的所有乘客
    public List<PassengerQueryResp> queryMine(){
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.setOrderByClause("name asc");
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        criteria.andMemberIdEqualTo(LoginMemberContext.getId());
        List<Passenger> passengers = passengerMapper.selectByExample(passengerExample);
        return BeanUtil.copyToList(passengers , PassengerQueryResp.class);
    }

}
